package sport

import (
	"github.com/bgmp/config"
	"github.com/bgmp/middleware"
	"github.com/bgmp/model"
	"github.com/gin-gonic/gin"
	"time"
)

const Per_Chicken_Leg = 150

func (b Base) ReportCalorie(c *gin.Context, req *ReportCalorieRequest) *ReportCalorieResponse {
	req.EndTime.Time = time.Date(req.EndTime.Time.Year(), req.EndTime.Month(), req.EndTime.Day(), 23, 59, 59, 0, req.EndTime.Time.Location())
	response := ReportCalorieResponse{}

	delta := -24 * time.Hour * 7

	start := req.EndTime.Time.Add(delta)
	end := req.EndTime.Time

	userId := middleware.GetUserId(c)

	response.Days = model.GetDataDaysCount(config.DB, start, end, userId)
	response.Calorie = model.GetTotalCalorie(config.DB, start, end, userId)
	response.ChickLeg = response.Calorie / Per_Chicken_Leg

	start = start.Add(delta)
	end = end.Add(delta)
	response.Change = model.GetTotalCalorie(config.DB, start, end, userId)
	response.Change = response.Calorie - response.Change

	return &response
}

type ReportCalorieRequest struct {
	EndTime model.Time `json:"end_time"`
}

type ReportCalorieResponse struct {
	Days     uint    `json:"days"`
	Calorie  float64 `json:"calorie"`
	Change   float64 `json:"change"`
	ChickLeg float64 `json:"chick_leg"`
}
